package com.blb.edufileuploadservice.controller;

import com.github.tobato.fastdfs.domain.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * 文件上传服务
 */
@Slf4j
@CrossOrigin
@RestController
public class UploadController {

    @Autowired
    private FastFileStorageClient fastClient;

    @RequestMapping("/upload")
    public ResponseEntity<String> upload(MultipartFile file) throws IOException {
        //截取后缀名
        String extension = FilenameUtils.getExtension(file.getOriginalFilename());
        //上传文件
        StorePath storePath = fastClient.uploadFile(file.getInputStream(), file.getSize(), extension, null);
        log.info("保存文件：" + storePath.getFullPath());
        return ResponseEntity.ok("http://192.168.223.166/" + storePath.getFullPath());
    }
}
